package com.oldroid.apps.getit.preferences;

import com.oldroid.apps.getit.R;
import com.oldroid.apps.getit.util.DropBox;
import com.oldroid.apps.getit.util.LoginCredAsync;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class CustomListPreference extends ListPreference {
	Dialog mDialog;
	Context mContext;
	boolean isPass;
	private final String tag = "CustomListPreference";
	String test; // COMMENT THIS OUT LATER
	private SharedPreferences mPreferences;
	protected String pass;
	protected String user;

	public CustomListPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	public void onDialogClosed(boolean positiveResult) {
		test = getValue();
		super.onDialogClosed(positiveResult);
		if (positiveResult == false)
			setValue(test);

		if (getValue().equals("dropbox")
				&& positiveResult == true) {

			mDialog = new Dialog(mContext);

			mDialog.setContentView(R.layout.popupwindow);
			mDialog.setTitle(R.string.pref_upload_dropbox_dialog_title);

			EditText login = (EditText) mDialog.findViewById(R.id.login);
			EditText password = (EditText) mDialog.findViewById(R.id.password);

			Button loginButton = (Button) mDialog
					.findViewById(R.id.LoginButton);
			Button cancelButton = (Button) mDialog
					.findViewById(R.id.CancelLoginButton);

			mDialog.show();

			cancelButton.setOnClickListener(new OnClickListener() {

				// @Override
				public void onClick(View v) {
					setValue(test);
					mDialog.dismiss(); // cancel dialogs
				}
			});

			loginButton.setOnClickListener(new OnClickListener() {

				// @Override
				public void onClick(View v) {
					// Login to dropbox
					// showToast("Attempting Dropbox login");

					// Get username and password entered
					EditText login = (EditText) mDialog
							.findViewById(R.id.login);
					EditText password = (EditText) mDialog
							.findViewById(R.id.password);

					// To be done later : basic user-input checking here

					// Get username & passsword
					user = login.getText().toString();
					pass = password.getText().toString();
					boolean isValid = hasNameAndDomain(user);
					Log.i(tag, "id: " + user + " pass: " + pass + " isValid : " + isValid);
					
					// Try to see if we have logged in before and already
					// have
					// access tokens

					// Jervis, can you see why it's not finding MODE_PRIVATE
					if (!isValid || pass.length() <6) {
						Animation shake = AnimationUtils.loadAnimation(
								mContext, R.anim.shake);
						EditText temp = !isValid ? login : password;
						temp.startAnimation(shake);

					} else {
						isPass = true;
					}

					if (isPass) {
						SharedPreferences data = mContext.getSharedPreferences(
								DropBox.CRED, Context.MODE_PRIVATE);
						String dropbox_user = data.getString("DROPBOX_USER",
								null);
						String key = data.getString("ACCESS_KEY", null);
						String secret = data.getString("ACCESS_SECRET", null);

						LoginCredAsync loginTask = new LoginCredAsync(mContext,
								user, pass);
						loginTask.execute();

						mDialog.dismiss(); // close dialogs
					}
				}

			});
		} else if (this.getValue().equals("filename")
				&& positiveResult == true) {
			mPreferences = PreferenceManager
					.getDefaultSharedPreferences(mContext);
			mDialog = new Dialog(mContext);
			mDialog.setContentView(R.layout.filenamespopup);
			mDialog.setTitle(R.string.pref_category_filename_dialog_title);
			// EditText login = (EditText) mDialog.findViewById(R.id.login);
			// EditText password = (EditText)
			// mDialog.findViewById(R.id.password);

			// Button loginButton = (Button)
			// mDialog.findViewById(R.id.LoginButton);
			Button cancelButton = (Button) mDialog
					.findViewById(R.id.CancelLoginButton);

			cancelButton.setOnClickListener(new OnClickListener() {

				// @Override
				public void onClick(View v) {
					setValue(test);
					mDialog.dismiss(); // cancel dialogs
				}
			});

			CheckBox firstLetter = (CheckBox) mDialog.findViewById(R.id.Letter);
			if (mPreferences.getString("Filename Choice", "NOVALUE").equals(
					"firstletter"))
				firstLetter.setChecked(true);
			firstLetter.setOnClickListener(new OnClickListener() {

				// @Override
				public void onClick(View v) {

					SharedPreferences.Editor edit = mPreferences.edit();
					edit.putString("Filename Choice", "firstletter");
					edit.commit();
					mDialog.dismiss(); // cancel dialogs
				}
			});

			CheckBox nameLength = (CheckBox) mDialog.findViewById(R.id.Length);
			if (mPreferences.getString("Filename Choice", "NOVALUE").equals(
					"namelength"))
				nameLength.setChecked(true);
			nameLength.setOnClickListener(new OnClickListener() {

				// @Override
				public void onClick(View v) {
					SharedPreferences.Editor edit = mPreferences.edit();
					edit.putString("Filename Choice", "namelength");
					edit.commit();
					mDialog.dismiss(); // cancel dialogs
				}
			});

			mDialog.show();

		}

	}
	
	private static boolean hasNameAndDomain(String emailAddress){
		String[] tokens = emailAddress.split("@");
		return tokens.length == 2 && tokens[1].contains(".");
	}

	public final void setValueToLocal() {
		setValue(getContext().getResources().getString(
				R.string.pref_upload_local));
	}

}
